from sqlalchemy.orm import Session
from database import SessionLocal, Base, engine
import time
from models import SysUser

Base.metadata.create_all(bind=engine)

def init_db():
    """
    初始化数据库表结构和默认数据
    """
    db: Session = SessionLocal()
    
    # 添加默认admin用户（使用SQLAlchemy ORM操作）
    admin_user = db.query(SysUser).filter(SysUser.username == 'admin').first()
    if not admin_user:
        new_admin = SysUser(
            username="admin",
            password="HycAdmin",
            created_at=int(time.time())
        )
        db.add(new_admin)
        db.commit()
        db.refresh(new_admin)
    db.close()
    